#include "SelectionBox.hpp"
#include "Colors.hpp"
#include "GLPrimitives.h"

// Ctr
SelectionBox::SelectionBox()
{
	m_OriginalStart.x = 0;
	m_OriginalStart.y = 0;
	m_OriginalEnd.x = 1;
	m_OriginalEnd.y = 1;

	GenerateCorrectedVectors();

	m_Color = black;
}

// Generate corrected vectors
void SelectionBox::GenerateCorrectedVectors()
{
	// Correct the position
	(m_OriginalStart.x <= m_OriginalEnd.x) ? (m_CorrectedStart.x = m_OriginalStart.x, m_CorrectedEnd.x = m_OriginalEnd.x) : 
		(m_CorrectedStart.x = m_OriginalEnd.x, m_CorrectedEnd.x = m_OriginalStart.x);

	(m_OriginalStart.y <= m_OriginalEnd.y) ? (m_CorrectedStart.y = m_OriginalStart.y, m_CorrectedEnd.y = m_OriginalEnd.y) : 
		(m_CorrectedStart.y = m_OriginalEnd.y, m_CorrectedEnd.y = m_OriginalStart.y);
}
// Render's the selection box
void SelectionBox::Render(ClippingPlane *pClipPlane)
{
	

	// Render based on the corrected position, and render based on screen coordinates
	DrawPrimitives->DrawRectangle(m_CorrectedStart.x - pClipPlane->GetX(), m_CorrectedStart.y - pClipPlane->GetY(),
		m_CorrectedEnd.x - pClipPlane->GetX(), m_CorrectedEnd.y - pClipPlane->GetY(), m_Color);
	DrawPrimitives->DrawCircle(m_OriginalStart.x - pClipPlane->GetX(), m_OriginalStart.y - pClipPlane->GetY(), 2, yellow);
}

bool SelectionBox::InBounds(const Vector2D& position)
{
	SDL_Rect big;
	big.x = m_CorrectedStart.x;
	big.y = m_CorrectedStart.y;
	big.w = m_CorrectedEnd.x - m_CorrectedStart.x;
	big.h = m_CorrectedEnd.y - m_CorrectedStart.y;

	if((position.x > big.x) && (position.x < big.x + big.w) &&
		(position.y > big.y) && (position.y < big.y + big.h))
		return true;

	return false;
}

bool SelectionBox::OutOfBounds(const Vector2D& position)
{
	return !InBounds(position);
}